﻿using System;
using System.Linq;
using Coolite.Ext.Web;
using Pap.Web.Modules.Reports.ViewModel;
using Pap.Web.Utils;

namespace Pap.Web.Modules.Reports.Views
{
    /// <summary>
    /// 
    /// </summary>
    public partial class TotalSystemReport : BasePage<TotalSystemReportViewModel>
    {
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Master.GenerateReport += (Master_GenerateReport);
            if (!Ext.IsAjaxRequest)
            {
                FillReportParams();
            }
        }

        protected void Master_GenerateReport(object sender, EventArgs e)
        {
            //get search paramters
            int? customerType = null;
            if (!string.IsNullOrEmpty(cmbCustomerType.SelectedItem.Value))
                customerType = Convert.ToInt32(cmbCustomerType.SelectedItem.Value);

            Guid educationLevelID = string.IsNullOrEmpty(cmbEducationalLevel.SelectedItem.Value) ? Guid.Empty : new Guid(cmbEducationalLevel.SelectedItem.Value);
            var selectedProjects = lstProjects.SelectedItems.ToList().Select(x => x.Value).ToList();
            Guid projectStageID = string.IsNullOrEmpty(cmbProjectStages.SelectedItem.Value) ? Guid.Empty : new Guid(cmbProjectStages.SelectedItem.Value);
            Guid socialStatusID = string.IsNullOrEmpty(cmbSocialStatus.SelectedItem.Value) ? Guid.Empty : new Guid(cmbSocialStatus.SelectedItem.Value);

            int ageFrom = int.MinValue;
            int ageTo = int.MaxValue;

            if (!string.IsNullOrEmpty(txtAgeFrom.Text))
                ageFrom = Convert.ToInt32(txtAgeFrom.Text);

            if (!string.IsNullOrEmpty(txtAgeTo.Text))
                ageTo = Convert.ToInt32(txtAgeTo.Text);

            string job = txtJob.Text;

            int familyMembersFrom = int.MinValue;
            int familyMembersTo = int.MaxValue;

            if (!string.IsNullOrEmpty(txtFamilyMembersFrom.Text))
                familyMembersFrom = Convert.ToInt32(txtFamilyMembersFrom.Text);

            if (!string.IsNullOrEmpty(txtFamilyMembersTo.Text))
                familyMembersTo = Convert.ToInt32(txtFamilyMembersTo.Text);

            int familyMonthlyIncomeRangeFrom = int.MinValue;
            int familyMonthlyIncomeRangeTo = int.MaxValue;

            if (!string.IsNullOrEmpty(txtFamilyMonthlyIncomeRangeFrom.Text))
                familyMonthlyIncomeRangeFrom = Convert.ToInt32(txtFamilyMonthlyIncomeRangeFrom.Text);

            if (!string.IsNullOrEmpty(txtFamilyMonthlyIncomeRangeTo.Text))
                familyMonthlyIncomeRangeTo = Convert.ToInt32(txtFamilyMonthlyIncomeRangeTo.Text);


            Guid loanTypeID = string.IsNullOrEmpty(cmbLoanTypes.SelectedItem.Value) ? Guid.Empty : new Guid(cmbLoanTypes.SelectedItem.Value);

            bool? loanStatus = null;
            if (!string.IsNullOrEmpty(cmbLoanStatus.SelectedItem.Value))
                loanStatus = Convert.ToBoolean(cmbLoanStatus.SelectedItem.Value);

            Guid legalFormID = string.IsNullOrEmpty(cmbLegalForms.SelectedItem.Value) ? Guid.Empty : new Guid(cmbLegalForms.SelectedItem.Value);
            Guid loanPlaceTypeID = string.IsNullOrEmpty(cmbLoanPlaceTypes.SelectedItem.Value) ? Guid.Empty : new Guid(cmbLoanPlaceTypes.SelectedItem.Value);

            Guid currentLoanStepID = string.IsNullOrEmpty(cmbLoanSteps.SelectedItem.Value) ? Guid.Empty : new Guid(cmbLoanSteps.SelectedItem.Value);

            int? paymentPeriod = null;
            if (!string.IsNullOrEmpty(txtPaymentPeriod.Text))
                paymentPeriod = Convert.ToInt32(txtPaymentPeriod.Text);

            int? loanExecutionAmount = null;
            if (!string.IsNullOrEmpty(txtLoanExecutionAmount.Text))
                loanExecutionAmount = Convert.ToInt32(txtLoanExecutionAmount.Text);

            DateTime? loanExecutionDateFrom = null;
            loanExecutionDateFrom = dtLoanExecutionDateFrom.SelectedDate;
            DateTime? loanExecutionDateTo = dtLoanExecutionDateTo.SelectedDate;

            DateTime? loanLastInstallmentDateFrom = dtLoanLastInstallmentDateFrom.SelectedDate;
            DateTime? loanLastInstallmentDateTo = dtLoanLastInstallmentDateTo.SelectedDate;

            DateTime? loanCreateDateFrom = dtLoanCreateDateFrom.SelectedDate;
            DateTime? loanCreateDateTo = dtLoanCreateDateTo.SelectedDate;

            string refuseReason = txtRefuseReason.Text;

            DateTime? boardDateFrom = dtBoardDateFrom.SelectedDate;
            DateTime? boardDateTo = dtBoardDateTo.SelectedDate;

            int boardNumberFrom = int.MinValue;
            int boardNumberTo = int.MaxValue;

            if (!string.IsNullOrEmpty(txtBoradNumberFrom.Text))
                boardNumberFrom = Convert.ToInt32(txtBoradNumberFrom.Text);

            if (!string.IsNullOrEmpty(txtBoradNumberTo.Text))
                boardNumberTo = Convert.ToInt32(txtBoradNumberTo.Text);

            string boardDecision = txtBoardDecision.Text;

            string postPoneReason = txtPostPoneReason.Text;

            var selectedCommittes = lstCommittees.SelectedItems.ToList().Select(x => x.Value).ToList();

            Guid responsibleID = string.IsNullOrEmpty(cmbResponsibles.SelectedItem.Value) ? Guid.Empty : new Guid(cmbResponsibles.SelectedItem.Value);

            this.ViewModel.FindTotalSystemReport(customerType,
                                                 educationLevelID,
                                                 selectedProjects,
                                                 projectStageID,
                                                 socialStatusID,
                                                 ageFrom,
                                                 ageTo,
                                                 job,
                                                 familyMembersFrom,
                                                 familyMembersTo,
                                                 familyMonthlyIncomeRangeFrom,
                                                 familyMonthlyIncomeRangeTo,
                                                 loanTypeID,
                                                 loanStatus,
                                                 legalFormID,
                                                 loanPlaceTypeID,
                                                 currentLoanStepID,
                                                 paymentPeriod,
                                                 loanExecutionAmount,
                                                 loanExecutionDateFrom,
                                                 loanExecutionDateTo,
                                                 loanLastInstallmentDateFrom,
                                                 loanLastInstallmentDateTo,
                                                 loanCreateDateFrom,
                                                 loanCreateDateTo,
                                                 refuseReason,
                                                 boardDateFrom,
                                                 boardDateTo,
                                                 boardNumberFrom,
                                                 boardNumberTo,
                                                 boardDecision,
                                                 postPoneReason,
                                                 selectedCommittes,
                                                 responsibleID);
        }

        ///// <summary>
        ///// Handles the GetReport event of the Master control.
        ///// </summary>
        ///// <param name="sender">The source of the event.</param>
        ///// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        //void Master_GetReport(object sender, EventArgs e)
        //{

        //    //get search paramters
        //    int? customerType = null;
        //    if (!string.IsNullOrEmpty(cmbCustomerType.SelectedItem.Value))
        //        customerType = Convert.ToInt32(cmbCustomerType.SelectedItem.Value);

        //    Guid educationLevelID = string.IsNullOrEmpty(cmbEducationalLevel.SelectedItem.Value) ? Guid.Empty : new Guid(cmbEducationalLevel.SelectedItem.Value);
        //    var selectedProjects = lstProjects.SelectedItems.ToList().Select(x => x.Value).ToList();
        //    Guid projectStageID = string.IsNullOrEmpty(cmbProjectStages.SelectedItem.Value) ? Guid.Empty : new Guid(cmbProjectStages.SelectedItem.Value);
        //    Guid socialStatusID = string.IsNullOrEmpty(cmbSocialStatus.SelectedItem.Value) ? Guid.Empty : new Guid(cmbSocialStatus.SelectedItem.Value);

        //    int ageFrom = int.MinValue;
        //    int ageTo = int.MaxValue;

        //    if (!string.IsNullOrEmpty(txtAgeFrom.Text))
        //        ageFrom = Convert.ToInt32(txtAgeFrom.Text);

        //    if (!string.IsNullOrEmpty(txtAgeTo.Text))
        //        ageTo = Convert.ToInt32(txtAgeTo.Text);

        //    string job = txtJob.Text;

        //    int familyMembersFrom = int.MinValue;
        //    int familyMembersTo = int.MaxValue;

        //    if (!string.IsNullOrEmpty(txtFamilyMembersFrom.Text))
        //        familyMembersFrom = Convert.ToInt32(txtFamilyMembersFrom.Text);

        //    if (!string.IsNullOrEmpty(txtFamilyMembersTo.Text))
        //        familyMembersTo = Convert.ToInt32(txtFamilyMembersTo.Text);

        //    int familyMonthlyIncomeRangeFrom = int.MinValue;
        //    int familyMonthlyIncomeRangeTo = int.MaxValue;

        //    if (!string.IsNullOrEmpty(txtFamilyMonthlyIncomeRangeFrom.Text))
        //        familyMonthlyIncomeRangeFrom = Convert.ToInt32(txtFamilyMonthlyIncomeRangeFrom.Text);

        //    if (!string.IsNullOrEmpty(txtFamilyMonthlyIncomeRangeTo.Text))
        //        familyMonthlyIncomeRangeTo = Convert.ToInt32(txtFamilyMonthlyIncomeRangeTo.Text);


        //    Guid loanTypeID = string.IsNullOrEmpty(cmbLoanTypes.SelectedItem.Value) ? Guid.Empty : new Guid(cmbLoanTypes.SelectedItem.Value);

        //    bool? loanStatus = null;
        //    if (!string.IsNullOrEmpty(cmbLoanStatus.SelectedItem.Value))
        //        loanStatus = Convert.ToBoolean(cmbLoanStatus.SelectedItem.Value);

        //    Guid legalFormID = string.IsNullOrEmpty(cmbLegalForms.SelectedItem.Value) ? Guid.Empty : new Guid(cmbLegalForms.SelectedItem.Value);
        //    Guid loanPlaceTypeID = string.IsNullOrEmpty(cmbLoanPlaceTypes.SelectedItem.Value) ? Guid.Empty : new Guid(cmbLoanPlaceTypes.SelectedItem.Value);

        //    Guid currentLoanStepID = string.IsNullOrEmpty(cmbLoanSteps.SelectedItem.Value) ? Guid.Empty : new Guid(cmbLoanSteps.SelectedItem.Value);

        //    int? paymentPeriod = null;
        //    if (!string.IsNullOrEmpty(txtPaymentPeriod.Text))
        //        paymentPeriod = Convert.ToInt32(txtPaymentPeriod.Text);

        //    int? loanExecutionAmount = null;
        //    if (!string.IsNullOrEmpty(txtLoanExecutionAmount.Text))
        //        loanExecutionAmount = Convert.ToInt32(txtLoanExecutionAmount.Text);

        //    DateTime? loanExecutionDateFrom = null;
        //    loanExecutionDateFrom = dtLoanExecutionDateFrom.SelectedDate;
        //    DateTime? loanExecutionDateTo = dtLoanExecutionDateTo.SelectedDate;

        //    DateTime? loanLastInstallmentDateFrom = dtLoanLastInstallmentDateFrom.SelectedDate;
        //    DateTime? loanLastInstallmentDateTo = dtLoanLastInstallmentDateTo.SelectedDate;

        //    DateTime? loanCreateDateFrom = dtLoanCreateDateFrom.SelectedDate;
        //    DateTime? loanCreateDateTo = dtLoanCreateDateTo.SelectedDate;

        //    string refuseReason = txtRefuseReason.Text;

        //    DateTime? boardDateFrom = dtBoardDateFrom.SelectedDate;
        //    DateTime? boardDateTo = dtBoardDateTo.SelectedDate;

        //    int boardNumberFrom = int.MinValue;
        //    int boardNumberTo = int.MaxValue;

        //    if (!string.IsNullOrEmpty(txtBoradNumberFrom.Text))
        //        boardNumberFrom = Convert.ToInt32(txtBoradNumberFrom.Text);

        //    if (!string.IsNullOrEmpty(txtBoradNumberTo.Text))
        //        boardNumberTo = Convert.ToInt32(txtBoradNumberTo.Text);

        //    string boardDecision = txtBoardDecision.Text;

        //    string postPoneReason = txtPostPoneReason.Text;

        //    var selectedCommittes = lstCommittees.SelectedItems.ToList().Select(x => x.Value).ToList();

        //    Guid responsibleID = string.IsNullOrEmpty(cmbResponsibles.SelectedItem.Value) ? Guid.Empty : new Guid(cmbResponsibles.SelectedItem.Value);

        //    ReportDataSource datasource = new ReportDataSource("dsTotalSystemReport",
        //           this.ViewModel.FindTotalSystemReport(customerType,
        //                                    educationLevelID,
        //                                    selectedProjects,
        //                                    projectStageID,
        //                                    socialStatusID,
        //                                    ageFrom,
        //                                    ageTo,
        //                                    job,
        //                                    familyMembersFrom,
        //                                    familyMembersTo,
        //                                    familyMonthlyIncomeRangeFrom,
        //                                    familyMonthlyIncomeRangeTo,
        //                                    loanTypeID,
        //                                    loanStatus,
        //                                    legalFormID,
        //                                    loanPlaceTypeID,
        //                                    currentLoanStepID,
        //                                    paymentPeriod,
        //                                    loanExecutionAmount,
        //                                    loanExecutionDateFrom,
        //                                    loanExecutionDateTo,
        //                                    loanLastInstallmentDateFrom,
        //                                    loanLastInstallmentDateTo,
        //                                    loanCreateDateFrom,
        //                                    loanCreateDateTo,
        //                                    refuseReason,
        //                                    boardDateFrom,
        //                                    boardDateTo,
        //                                    boardNumberFrom,
        //                                    boardNumberTo,
        //                                    boardDecision,
        //                                    postPoneReason,
        //                                    selectedCommittes,
        //                                    responsibleID).Tables[0]);

        //    rpvTotalSystemReport.LocalReport.DataSources.Clear();
        //    rpvTotalSystemReport.LocalReport.DataSources.Add(datasource);
        //    rpvTotalSystemReport.LocalReport.Refresh();
        //}

        /// <summary>
        /// Fills the report params.
        /// </summary>
        private void FillReportParams()
        {
            cmbCustomerType.Items.Clear();
            Coolite.Ext.Web.ListItem cmbCustomerTypeitem1 = new Coolite.Ext.Web.ListItem("جديد", "0");
            Coolite.Ext.Web.ListItem cmbCustomerTypeitem2 = new Coolite.Ext.Web.ListItem("مجدد", "1");
            cmbCustomerType.Items.Add(cmbCustomerTypeitem1);
            cmbCustomerType.Items.Add(cmbCustomerTypeitem2);

            cmbEducationalLevel.Items.Clear();
            strEducationalLevels.DataSource = this.ViewModel.GetAllEducationalLevels();
            strEducationalLevels.DataBind();

            lstProjects.Items.Clear();
            strProjects.DataSource = this.ViewModel.GetAllProjects();
            strProjects.DataBind();


            cmbProjectStages.Items.Clear();
            strProjectStages.DataSource = this.ViewModel.GetAllProjectStages();
            strProjectStages.DataBind();

            cmbSocialStatus.Items.Clear();
            strSocialStatus.DataSource = this.ViewModel.GetAllSocialStatuses();
            strSocialStatus.DataBind();


            cmbLoanTypes.Items.Clear();
            strLoanTypes.DataSource = this.ViewModel.GetAllLoanTypes();
            strLoanTypes.DataBind();

            cmbLoanStatus.Items.Clear();
            Coolite.Ext.Web.ListItem cmbLoanStatusitem1 = new Coolite.Ext.Web.ListItem("جديد", "false");
            Coolite.Ext.Web.ListItem cmbLoanStatusitem2 = new Coolite.Ext.Web.ListItem("قائم", "true");
            cmbLoanStatus.Items.Add(cmbLoanStatusitem1);
            cmbLoanStatus.Items.Add(cmbLoanStatusitem2);

            cmbLegalForms.Items.Clear();
            strLegalForms.DataSource = this.ViewModel.GetAllLoanLegalForms();
            strLegalForms.DataBind();

            cmbLoanPlaceTypes.Items.Clear();
            strLoanPlaceTypes.DataSource = this.ViewModel.GetAllLoanPlaces();
            strLoanPlaceTypes.DataBind();

            cmbLoanSteps.Items.Clear();
            strLoanSteps.DataSource = this.ViewModel.GetAllLoanSteps();
            strLoanSteps.DataBind();

            lstCommittees.Items.Clear();
            strCommittee.DataSource = this.ViewModel.GetAllCommittees();
            strCommittee.DataBind();

            cmbResponsibles.Items.Clear();
            strResponsibles.DataSource = this.ViewModel.GetAllResponsibles();
            strResponsibles.DataBind();

        }
    }
}
